'''
plot mean value of wrf output as line chart
'''
# -*- coding: utf-8 -*-
import os
import matplotlib.pyplot as plt
import wrf_output
import gauge


def Decumulation(seq):
    result = [seq[i + 1] - seq[i] for i in range(len(seq) - 1)]
    result.insert(0, seq[0])
    return result


def PlotMean(mean, labels):
    '''plot rainfall data of center'''
    plt.figure(figsize=(10, 10))
    for i in range(len(mean)):
        plt.plot(mean[-1].index, mean[i], label=labels[i])

    plt.legend(loc='best', fontsize='xx-large')
    plt.show()


def main(path, domain, start_time, end_time):
    # domains = ['01', '02']
    # get every mp_cu group wrf_output and extract the center value
    mp = ['2', '6', '7']
    cu = ['0', '1', '2', '7']
    pbl = ['2', '6', '9']
    results = []
    labels = []
    for i in mp:
        for j in cu:
            for k in pbl:
                mp_cu_pbl = i + j + k
                if os.path.exists(path + '/' + mp_cu_pbl + '/'):
                    output = path + '/' + mp_cu_pbl + '/wrfout_d' + domain + '_' + start_time
                    labels.append(mp_cu_pbl)
                    mean = wrf_output.GetMean(output)
                    mean = Decumulation(mean)
                    results.append(mean)

    # get the gauge value of the center
    gauge_mean = gauge.Get50GaugeMean(start_time, end_time)
    results.append(gauge_mean)
    labels.append('gauge')

    # plot the band and the gauge value
    PlotMean(results, labels)


if __name__ == "__main__":
    # new run
    # R1
    path = r'H:/research/rainfall_estimation/wrf_output/2008/01/1700/103090'
    domain = r'03'
    start_time = r'2008-01-17_00_00_00'
    end_time = r'2008-01-19_12_00_00'
    # event = 'R1'

    # R2
    # path = r'F:/Research/rainfall_estimation/WRF_Output/2008/01/1912'
    # domain = r'03'
    # start_time = r'2008-01-19_12_00_00'
    # end_time = r'2008-01-22_00_00_00'
    # event = 'R2'

    # R3
    # path = r'F:/Research/rainfall_estimation/WRF_Output/2008/03/1500'
    # domain = r'03'
    # start_time = r'2008-03-15_00_00_00'
    # end_time = r'2008-03-16_12_00_00'
    # event = 'R3'

    # R4
    # path = r'F:/Research/rainfall_estimation/WRF_Output/2008/08/1718'
    # domain = r'03'
    # start_time = r'2008-08-17_18_00_00'
    # end_time = r'2008-08-20_00_00_00'
    # event = 'R3'

    # R5
    # path = r'F:/Research/rainfall_estimation/WRF_Output/2008/09/2900'
    # domain = r'03'
    # start_time = r'2008-09-29_00_00_00'
    # end_time = r'2008-10-02_06_00_00'
    # event = 'R4'

    # R6
    # path = r'F:/Research/rainfall_estimation/WRF_Output/2008/12/0318'
    # domain = r'03'
    # start_time = r'2008-12-03_18_00_00'
    # end_time = r'2008-12-06_00_00_00'
    # event = 'R6'

    # R7
    # path = r'F:/Research/rainfall_estimation/WRF_Output/2008/12/1200'
    # domain = r'03'
    # start_time = r'2008-12-12_00_00_00'
    # end_time = r'2008-12-14_06_00_00'
    # event = 'R7'

    # main(path, domain, start_time, end_time, event)
    main(path, domain, start_time, end_time)
